package com.itheima.kafka.simple;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * 消息生产者代码
 * @作者 itcast
 * @创建日期 2021/7/9 9:42
 **/
public class ProducerFastStart {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //Kafka生产者的配置
        Properties config = new Properties();
        //Kafka 服务端地址
        config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:9092");
        //key的序列
        config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        //value的序列
        config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //Kafka生产者对象
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(config);
        //消息对象
        ProducerRecord<String, String> record = new ProducerRecord<String, String>("heima-001","message-key001","hello 你好");
        //发送消息
        RecordMetadata recordMetadata = kafkaProducer.send(record).get();
        System.out.println("分区：" + recordMetadata.partition() + "    偏移量：" + recordMetadata.offset());
        //关闭
        kafkaProducer.close();

    }



}
